/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.enterprise.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import org.crystalrange.enterprise.model.StorageSection;
import org.crystalrange.enterprise.service.StorageSectionService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class StorageSectionSelectController extends GenericForwardComposer {

    protected StorageSectionService storageSectionService;
    protected StorageSection selectedStorageSection;
    protected AnnotateDataBinder binder;
    protected Search searchStorageSection;
    protected List<StorageSection> storageSections;

    public StorageSectionService getStorageSectionService() {
        return this.storageSectionService;
    }

    public void setStorageSectionService(StorageSectionService storageSectionService) {
        this.storageSectionService = storageSectionService;
    }

    public StorageSection getSelectedStorageSection() {
        return this.selectedStorageSection;
    }

    public void setSelectedStorageSection(StorageSection selectedStorageSection) {
        this.selectedStorageSection = selectedStorageSection;
    }

    public List<StorageSection> getStorageSections() {
        return this.storageSections;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchStorageSection = new Search(StorageSection.class);
        this.searchStorageSection.setMaxResults(100);
        this.searchStorageSection.addFetch("warehouseNumber");
        this.searchStorageSection.addFetch("storageType");
        this.searchStorageSection.addFetch("createdBy");
        this.searchStorageSection.addFetch("updatedBy");
        this.storageSections = this.storageSectionService.search(this.searchStorageSection);

        component.setAttribute("controller", this);
    }

    public void onClick$buttonOk(Event event) throws Exception {
        if (this.selectedStorageSection != null) {
            this.execution.setAttribute("selectedStorageSection", this.selectedStorageSection);
            this.execution.setAttribute("result", "ok");
            this.self.detach();
        } else {
            Messagebox.show("Please select a Storage Section.");
        }
    }

    public void onClick$buttonCancel(Event event) throws Exception {
        this.execution.setAttribute("selectedStorageSection", null);
        this.execution.setAttribute("result", "cancel");
        this.self.detach();
    }

    public void onClick$buttonSearch(Event event) throws Exception {
        this.execution.setAttribute("searchStorageSection", this.searchStorageSection);

        Window window = (Window) this.execution.createComponents("~./zul/html/storageSectionSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchStorageSection = (Search) this.execution.getAttribute("searchStorageSection");
        }

        this.storageSections = this.storageSectionService.search(this.searchStorageSection);
        this.binder.loadAll();
    }
}
